package com.prabhat.webapp.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import org.apache.cxf.jaxrs.model.wadl.Description;
import org.apache.cxf.jaxrs.model.wadl.Descriptions;
import org.apache.cxf.jaxrs.model.wadl.DocTarget;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.prabhat.webapp.enums.PKErrorInterface;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
		name = "BaseResponse", 
		propOrder = {"refId", "localeTime", "errorDetails", "usrId"},
		namespace="http://www.gharari.com"
		)
public class BaseResponse  implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
	protected String refId;
	
	@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
	@XmlSchemaType(name = "dateTime")
	protected Date localeTime;
	
	@XmlElementWrapper(name="errorDetails")
	@XmlElements (
			@XmlElement(name = "errorDetail", type = ErrorBean.class)
			)
	protected List<ErrorBean> errorDetails;

	@XmlTransient @JsonIgnore
	private Integer code;
	
	@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
	private Long usrId;

	@XmlTransient @JsonIgnore
	private String description;
	
	@XmlTransient @JsonIgnore
	private Integer tempCode=null;

	@XmlTransient @JsonIgnore
	private String tempDescription = "";
	
	public void addErr(String errCode) {
		String errDesc="";
		if("0".equals(errCode))
			errDesc="Success";
		addErr(errCode,errDesc);
	}
	
	public void addErr(List<ErrorBean> errList) {
		for(ErrorBean errbean: errList){
			addErr(errbean.getErrorCode(),errbean.getErrorDescription());
		}
	}
	
	public void addErr(String errCode, String message) {
		if (errorDetails == null) {
			errorDetails = new ArrayList<ErrorBean>();
			setErrorDetails(errorDetails);
		}
		ErrorBean err = new ErrorBean();
		err.setErrorCode(errCode);
		err.setErrorDescription(message);
		errorDetails.add(err);
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		if(errorDetails==null || errorDetails.size()==0)return "[]";
		for(ErrorBean eb: errorDetails) {
			builder.append(" [errocode:").append(eb.errorCode).append(" desc:").append(eb.errorDescription).append("]");
		}
		return builder.toString();
	}
	
	/**
	 * Gets the value of the refId property.
	 * 
	 */
	@Descriptions({
		@Description(value = "Reference id sent in the request.", target = DocTarget.PARAM),
		@Description(value = "T07130000067", target = DocTarget.PARAM)
	})
	public String getRefId() {
		return refId;
	}

	/**
	 * Sets the value of the refId property.
	 * 
	 */
	public void setRefId(String value) {
		this.refId = value;
	}

	@Descriptions({
		@Description(value = "Time in milliseconds at which request is processed.", target = DocTarget.PARAM),
		@Description(value = "1395110672709", target = DocTarget.PARAM)
	})
	public Date getLocaleTime() {
		return localeTime;
	}

	/**
	 * @param localeTime the localeTime to set
	 */
	public void setLocaleTime(Date localeTime) {
		this.localeTime = localeTime;
	}

	/**
	 * @return the errorDetails
	 */
	@Descriptions({
		@Description(value = "List of error details", target = DocTarget.PARAM),
		@Description(value = "[{'code':'200','description':'success'}] etc", target = DocTarget.PARAM)
	})
	@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
	public List<ErrorBean> getErrorDetails() {
		return errorDetails;
	}

	/**
	 * @param errorDetails the errorDetails to set
	 */
	public void setErrorDetails(List<ErrorBean> errorDetails) {
		this.errorDetails = errorDetails;
	}

	/**
	 * @return the code
	 */
	@Descriptions({
		@Description(value = "Code of the error details if any or success", target = DocTarget.PARAM),
		@Description(value = "1 or 2 or 3 etc", target = DocTarget.PARAM)
	})
	public Integer getCode() {
		
		/*if(tempCode!=null && tempCode==0){
			return code;
		}else{
			return null;
		}*/
		Integer code = null;
		if(errorDetails != null && errorDetails.size() > 0){
			ErrorBean errorBean = errorDetails.get(errorDetails.size() - 1);
			code = Integer.parseInt(errorBean.getErrorCode());
		}
		return code;
	}
	/**
	 * @return the description
	 */
	public String getDescription() {
		/*if(tempDescription==null){
			return description;
		}
		return null;*/
		String description = null;
		if(errorDetails != null && errorDetails.size() > 0){
			ErrorBean errorBean = errorDetails.get(errorDetails.size() - 1);
			description = errorBean.getErrorDescription();
		}
		return description;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(Integer code) {
	// putting this empty for reflection code to work in cardserviceimpl card load and unload.
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
	// putting this empty for reflection code to work in cardserviceimpl card load and unload.
	}

    @XmlTransient @JsonIgnore
    private Integer programOperationFlavourId;
	public Integer getProgramOperationFlavourId() {
		return programOperationFlavourId;	
	}

	public void setProgramOperationFlavourId(Integer programOperationFlavourId) {
		this.programOperationFlavourId = programOperationFlavourId;
	}	


  /**
  * Helper method which just takes a enum status code
  **/
  public void setStatus (PKErrorInterface enumval) {
    List<ErrorBean> errorBeanList = getErrorDetails();
    if(errorBeanList == null){
      errorBeanList = new ArrayList<ErrorBean>();
    }
    ErrorBean errorBean = new ErrorBean();
    errorBean.setErrorCode(""+enumval.getCode());
    errorBean.setErrorDescription(enumval.errdescription());
    errorBeanList.add(errorBean);
    setErrorDetails(errorBeanList);
  }

  @Descriptions({
		@Description(value = "Unique id of the user, which will be used in making requests.", target = DocTarget.PARAM),
		@Description(value = "13212", target = DocTarget.PARAM)
	})
	public Long getUsrId() {
		return usrId;
	}
	
	public void setUsrId(Long usrId) {
		this.usrId = usrId;
	}
	
}
